package cn.lanqiao.bigDate.service.data.impl;

import cn.lanqiao.bigDate.entity.data.DataBook;
import cn.lanqiao.bigDate.entity.data.DataBookOrder;
import cn.lanqiao.bigDate.entity.data.DataStock;
import cn.lanqiao.bigDate.entity.data.result.ResultBookSeller;
import cn.lanqiao.bigDate.mapper.data.*;
import cn.lanqiao.bigDate.service.data.DataSellerListService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DataSellerListServiceImpl implements DataSellerListService {
    @Resource
    private DataStockMapper dataStockMapper;
    @Resource
    private DataBookOrderMapper dataBookOrderMapper;
    @Resource
    private DataBookMapper dataBookMapper;

    @Override
    public List<ResultBookSeller> findSellerList() {
        //查询出库存数据
        List<DataStock> stocks = dataStockMapper.selectStockGroupByMonth();
        //查询出销售数据
        List<DataBookOrder> orders = dataBookOrderMapper.selectOrderGroupyMonth();

        //根据isbn码获取书名
        List<String> stocksIsbn = stocks.stream().map(DataStock::getIsbn).collect(Collectors.toList());
        List<String> ordersIsbn = orders.stream().map(DataBookOrder::getIsbn).collect(Collectors.toList());

        //获取畅销售和滞销书所有书本信息
        QueryWrapper qw = new QueryWrapper();
        qw.in("isbn",ordersIsbn);
        qw.or();
        qw.in("isbn",stocksIsbn);
        List<DataBook> bookList = dataBookMapper.selectList(qw);
        //将所有书本转成成key value
        Map<String, DataBook> bookMap = bookList.stream().collect(Collectors.toMap(DataBook::getIsbn, DataBook -> DataBook));

        //根据月份对滞销书分组
        Map<String,List<DataStock>> stocksMap = stocks.stream().collect(Collectors.groupingBy(DataStock::getMonth));
        Map<String,List<DataBookOrder>> ordersMap = orders.stream().collect(Collectors.groupingBy(DataBookOrder::getMonth));
        List<ResultBookSeller> list = new ArrayList<>();
        for(String key:ordersMap.keySet()){
            ResultBookSeller rbs=new ResultBookSeller();
            //添加滞销书数据
            List<DataStock> stocksMonth = stocksMap.get(key);
            if(stocksMonth!=null && stocksMonth.size()>0){
                int num=0;
                List <String> dsBookName=new ArrayList<>();
                for(DataStock ds:stocksMonth){
                    String isbn = ds.getIsbn();
                    DataBook dataBook = bookMap.get(isbn);
                    if(dataBook!=null){
                        num=num+ds.getStock();
                        String name = dataBook.getName();
                        dsBookName.add(name);
                    }
                }
                rbs.setUnmarketableList(dsBookName);
                rbs.setInventoryData(num);


                //添加畅销书数据
                List<DataBookOrder> orderMonth = ordersMap.get(key);
                if(orderMonth!=null && orderMonth.size()>0){
                    int num1=0;
                    List<String> orderBookName=new ArrayList<>();
                    for(DataBookOrder dbom:orderMonth){
                        String isbn = dbom.getIsbn();
                        DataBook dataBook = bookMap.get(isbn);
                        if(dataBook!=null){
                            num1=num1+dbom.getQuantity();
                            String name = dataBook.getName();
                            orderBookName.add(name);
                        }
                    }
                    rbs.setBestSellingList(orderBookName);
                    rbs.setSalesData(num);
                }
            }
            list.add(rbs);

        }
        System.out.println("畅销榜，滞销榜");
        System.out.println(list);
//        return Arrays.asList(list.get(7));
        return list;
    }






}
